package com.yuyou.train.lesson.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.train.lesson.mapper.ResourceQuestionMapper;
import com.yuyou.train.lesson.service.ILessonResourceRuleFrameService;
import com.yuyou.train.lesson.service.ILessonService;
import com.yuyou.train.lesson.service.IResourceExamItemService;
import com.yuyou.train.lesson.service.IResourceExamService;
import com.yuyou.train.lesson.service.IResourceQuestionService;
import com.yuyou.train.lesson.vo.req.ReqResQuesAdd;
import com.yuyou.train.lesson.vo.req.ReqResQuesUpdate;
import com.yuyou.train.service.api.lesson.dto.QuestionPageDTO;
import com.yuyou.train.service.api.lesson.entity.Lesson;
import com.yuyou.train.service.api.lesson.entity.LessonResourceRuleFrame;
import com.yuyou.train.service.api.lesson.entity.ResourceExam;
import com.yuyou.train.service.api.lesson.entity.ResourceExamItem;
import com.yuyou.train.service.api.lesson.entity.ResourceQuestion;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.constant.lesson.ResourceQuestionCons;
import com.yuyou.train.util.message.lesson.LessonMess;
import com.yuyou.train.util.message.lesson.QuestionMess;
import com.yuyou.train.util.tool.BeanUtils;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.DefindBeanUtils;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class ResourceQuestionServiceImpl implements IResourceQuestionService{

	@Autowired
	private ResourceQuestionMapper resourceQuestionMapper;
	
	@Autowired
	private IResourceExamItemService resourceExamItemService;
	
	@Autowired
	private ILessonResourceRuleFrameService lesResRulFraService;
	
	@Autowired
	private IResourceExamService resourceExamService;
	
	@Autowired
	private ILessonService lessonService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return resourceQuestionMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(ResourceQuestion record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return resourceQuestionMapper.insert(record);
	}

	@Override
	public ResourceQuestion selectByPrimaryKey(String id) {
		return resourceQuestionMapper.selectByPrimaryKey(id);
	}

	@Override
	public ResourceQuestion updateByPrimaryKeySelective(ResourceQuestion record) {
		record.setUpdateTime(new Date());
		resourceQuestionMapper.updateByPrimaryKeySelective(record);
		return record;
	}

	@Override
	@Transactional(timeout=60)
	public ResBoolSimpleInfo<String> addQuestion(ReqResQuesAdd reqResQuesAdd, String userId) {
		// 保存题目基本信息
		ResourceQuestion resourceQuestion = new ResourceQuestion();
		DefindBeanUtils.copyProperties(reqResQuesAdd, resourceQuestion);

		resourceQuestion.setId(Sequence.getSequence().getNextId());
		resourceQuestion.setNumber(this.generateNum());
		resourceQuestion.setUpdateUser(userId);
		resourceQuestion.setCreateTime(DateUtil.now());
		resourceQuestion.setUpdateTime(DateUtil.now());
		this.insert(resourceQuestion);

		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResBoolSimpleInfo<String> delQuestion(String id) {
		
		ResBoolSimpleInfo<String> res=hasLessonRel(id);
		if(!res.isSuccess()){
			return res;
		}		
		this.deleteByPrimaryKey(id);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public List<QuestionPageDTO> findQuestionPageList(
			Pagination<QuestionPageDTO> p) {
		return resourceQuestionMapper.findQuestionPageList(p);
	}

	@Override
	public int findQuestionPageCount(Pagination<QuestionPageDTO> p) {
		return resourceQuestionMapper.findQuestionPageCount(p);
	}

	@Override
	@Transactional(timeout=60)
	public String generateNum() {
		String ex=DateUtil.format(new Date(), DateUtil.yyyyMMdd);			
		String num=resourceQuestionMapper.selectMaxNumberLock(ex);
		if(StringUtils.isEmpty(num)){
			return ex+ResourceQuestionCons.MIN_QUESTION_NUMBER;
		}else{
			long numLong=Long.parseLong(num)+1;
			return String.valueOf(numLong);
		}
	}

	@Override
	public int batchUpdateStatus(List<String> ids, String isUse) {
		return resourceQuestionMapper.batchUpdateStatus(ids, isUse);
	}
	
	@Override
	public ResBoolSimpleInfo<String> hasLessonRel(String id){	
		ResBoolSimpleInfo<String> resStr = new ResBoolSimpleInfo<String>();
		//试卷
		List<ResourceExamItem> examItemList=resourceExamItemService.findExamItemByQuestionId(id);
		if(examItemList.size()>0){			
			StringBuilder lesName=new StringBuilder();
			examItemList.forEach(item -> {
				ResourceExam exam=resourceExamService.selectByPrimaryKey(item.getResourceExamId());
				lesName.append(exam.getName()+";");
				});		
			resStr.setSuccess(false);
			resStr.setMessage(String.format(QuestionMess.ERR_DEL_HAS_EXAM,lesName.toString()));
			return resStr;
		}		
		//视频帧
		List<LessonResourceRuleFrame> frameList=lesResRulFraService.findFrameByQuestionId(id);
		if(frameList.size()>0){
			StringBuilder lesName=new StringBuilder();
			frameList.forEach(item -> {
				Lesson l=lessonService.selectByPrimaryKey(item.getLessonId());
				lesName.append(l.getName()+";");
				});
			resStr.setSuccess(false);
			resStr.setMessage(String.format(LessonMess.ERR_UPDATE_HAS_LESS_RULE, lesName.toString()));
			return resStr;
		}
		
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResBoolSimpleInfo<ResourceQuestion> updateQuestion(ReqResQuesUpdate reqResQuesUpdate,
															  String userId) {
		ResBoolSimpleInfo<ResourceQuestion> res = new ResBoolSimpleInfo<>(true, null);
		
		ResourceQuestion resourceQuestion = new ResourceQuestion();
		DefindBeanUtils.copyProperties(reqResQuesUpdate, resourceQuestion);
		resourceQuestion.setUpdateUser(userId);
				

		ResBoolSimpleInfo<String> resStr=hasLessonRel(reqResQuesUpdate.getId());
		if(!resStr.isSuccess()){
			res.setSuccess(false);
			res.setMessage(resStr.getMessage());
			return res;
		}
		
		ResourceQuestion resourceQuestionRet = this.updateByPrimaryKeySelective(resourceQuestion);
		res.setData(resourceQuestionRet);				
		return res;
	}
	
	
	
}